#!/bin/sh

PREPARE=$1
LANG="$2"
CVERSION=4.6rc3
VERSION="$(cat data/eversion)"
CFG="tmp/eCore.cfg"
ISODIR="/mnt/sda2/eCore"
TESTDIR="/mnt/sda1/boot/iso"
THISDIR=`pwd`
CDLOCATION=/mnt/sda2/distro/Core-$CVERSION.iso
EZTMPDIR=/mnt/sda2/tmp/ezremaster

[ "$LANG" ] && LANG=_$LANG
if [ ! -f /usr/local/tce.installed/mkisofs-tools ]; then
   if [ ! -e /etc/sysconfig/tcedir/optional/mkisofs-tools.tcz ]; then
      tce-load -w mkisofs-tools
   fi
      tce-load -is mkisofs-tools
fi
if [ ! -f /usr/local/tce.installed/advcomp ]; then
   if [ ! -e /etc/sysconfig/tcedir/optional/advcomp.tcz ]; then
      tce-load -w advcomp
   fi
      tce-load -is advcomp
fi
if [ ! "$PREPARE" = "-np" ];then
	[ -f tmp/allextensions.lst ] && sudo rm 	   tmp/allextensions.lst    
	cp  data/instlist/standard.instlist    	   tmp/allextensions.lst    
	cat data/instlist/ondemand.instlist  	>> tmp/allextensions.lst
	cat data/instlist/ndiswrapper.instlist  >> tmp/allextensions.lst
	cat data/instlist/wififirmware.instlist >> tmp/allextensions.lst
	cat data/instlist/wifi.instlist 	>> tmp/allextensions.lst
	cat data/instlist/modem.instlist 	>> tmp/allextensions.lst
	echo Xorg-7.6-3d.tcz	  		>> tmp/allextensions.lst
	echo xf86-video-all.tcz	  		>> tmp/allextensions.lst
	echo Xvesa.tcz		  		>> tmp/allextensions.lst
	echo "cd_location = $CDLOCATION" 	   	  	  	>  $CFG
	echo "temp_dir = $EZTMPDIR"		  	  	  	>> $CFG
	echo "app_extract_initrd = exquisite.tcz" 	  		>> $CFG
	echo "app_extract_initrd = Xlibs.tcz" 	  		  	>> $CFG
	echo "extract_tcz_script = chroot" 	  		  	>> $CFG
	sed "s/^/app_outside_initrd_onboot = /" tmp/allextensions.lst	>> $CFG
#	echo -n "ezremaster cfg file created.Press Enter key."
#	read gagme
	sudo rm -rf $EZTMPDIR/*
	./remaster.sh $CFG checkimage
	./remaster.sh $CFG extractimage
	#./remaster.sh $CFG bootcode
	sleep 4
#	echo -n "Next apps.Press Enter key."
#	read gagme
	./remaster.sh $CFG apps
fi
sudo rm tmp/onboot/*
mkdir -p tmp/onboot

cp  data/instlist/standard.instlist 	   tmp/onboot/onboot.lst
echo connman1.tcz 	    		>> tmp/onboot/onboot.lst
echo Xorg-7.6-3d.tcz 	    		>> tmp/onboot/onboot.lst
echo xf86-video-all.tcz     		>> tmp/onboot/onboot.lst

cp  data/instlist/standard.instlist 	   tmp/onboot/onbootwifi.lst
cat data/instlist/ndiswrapper.instlist  >> tmp/onboot/onbootwifi.lst
cat data/instlist/wififirmware.instlist >> tmp/onboot/onbootwifi.lst
cat data/instlist/wifi.instlist 	>> tmp/onboot/onbootwifi.lst
echo connman1.tcz 	    		>> tmp/onboot/onbootwifi.lst
echo Xorg-7.6-3d.tcz 	    		>> tmp/onboot/onbootwifi.lst
echo xf86-video-all.tcz     		>> tmp/onboot/onbootwifi.lst

cp  data/instlist/standard.instlist 	   tmp/onboot/onbootmodem.lst
cat data/instlist/modem.instlist  	>> tmp/onboot/onbootmodem.lst
echo Xorg-7.6-3d.tcz 	    		>> tmp/onboot/onbootmodem.lst
echo xf86-video-all.tcz     		>> tmp/onboot/onbootmodem.lst


cp  data/instlist/standard.instlist 	   tmp/onboot/onbootxvesa.lst
echo connman1.tcz 	    		>> tmp/onboot/onbootxvesa.lst
echo Xvesa.tcz 		    		>> tmp/onboot/onbootxvesa.lst

cp  data/instlist/standard.instlist 	   tmp/onboot/onbootxvesawifi.lst
cat data/instlist/ndiswrapper.instlist  >> tmp/onboot/onbootxvesawifi.lst
cat data/instlist/wififirmware.instlist >> tmp/onboot/onbootxvesawifi.lst
cat data/instlist/wifi.instlist 	>> tmp/onboot/onbootxvesawifi.lst
echo connman1.tcz 	    		>> tmp/onboot/onbootxvesawifi.lst
echo Xvesa.tcz 		    		>> tmp/onboot/onbootxvesawifi.lst

cp  data/instlist/standard.instlist 	   tmp/onboot/onbootxvesamodem.lst
cat data/instlist/modem.instlist  	>> tmp/onboot/onbootxvesamodem.lst
echo Xvesa.tcz 	    			>> tmp/onboot/onbootxvesamodem.lst

#echo -n "onboot files created.Press Enter key."
#read gagme
sudo mv $EZTMPDIR/image/cde $EZTMPDIR/image/tce
if [ "$LANG" = "_it" ];then 
   sudo cp data/iso/boot_it.msg 	$EZTMPDIR/image/boot/isolinux/boot.msg
else
   sudo cp data/iso/isolinux.cfg 	$EZTMPDIR/image/boot/isolinux
   sudo cp data/iso/boot.msg 		$EZTMPDIR/image/boot/isolinux
   sudo cp data/iso/boot.msg 		$EZTMPDIR/image/boot/isolinux/f1
fi
sudo cp data/iso/menu.c32		$EZTMPDIR/image/boot/isolinux/
sudo cp data/iso/isolinux.bin		$EZTMPDIR/image/boot/isolinux/
sudo cp tmp/onboot/*.lst		$EZTMPDIR/image/tce
sudo cp data/scripts/tc-config   	$EZTMPDIR/extract/etc/init.d/
sudo cp data/scripts/e-tc-functions   	$EZTMPDIR/extract/etc/init.d/
sudo cp data/scripts/tce-setup   	$EZTMPDIR/extract/usr/bin/
sudo cp data/scripts/fromISOfile 	$EZTMPDIR/extract/usr/bin/
sudo cp data/scripts/filetool.sh 	$EZTMPDIR/extract/usr/bin/
sudo cp data/scripts/bootlocal.sh	$EZTMPDIR/extract/opt/
sudo cp remaster.sh		 	$EZTMPDIR/extract/usr/local/bin/
sudo cp data/eversion    		$EZTMPDIR/extract/usr/share/doc/tc/erelease.txt
sudo cp data/eCore.edj    		$EZTMPDIR/extract/usr/local/share/exquisite/data/themes/
sudo mkdir -p				$EZTMPDIR/extract/usr/local/lib/enlightenment/modules/wizard/extra_desktops/
sudo cp data/mplayer.desktop		$EZTMPDIR/extract/usr/local/lib/enlightenment/modules/wizard/extra_desktops/
sudo cp -a data/ondemand		$EZTMPDIR/image/tce
#sudo cp data/.xsession    		$EZTMPDIR/extract/etc/skel/
#sudo cp data/entrance.conf    		$EZTMPDIR/extract/usr/local/etc/entrance.conf
#sleep 4
echo -n "Next step package. Press Enter key."
read gagme
./remaster.sh $CFG package
cp $EZTMPDIR/ezremaster.iso "$TESTDIR"
sudo mv $EZTMPDIR/ezremaster.iso "$ISODIR/eCore-$VERSION.iso"

#echo -n "Next step CD version. Press Enter key."
#read gagme
#sudo mv $EZTMPDIR/image/tce $EZTMPDIR/image/cde
#sudo sed -i "s/quiet/quiet cde/" $EZTMPDIR/image/boot/isolinux/isolinux.cfg 	
#sudo mkisofs -l -J -R -V eCore -no-emul-boot -boot-load-size 4 \
#	-boot-info-table -b boot/isolinux/isolinux.bin \
#	-c boot/isolinux/boot.cat -o $EZTMPDIR/ezremaster.iso . || exit 24
#sudo mv $EZTMPDIR/ezremaster.iso "$ISODIR/eCoreCD-$VERSION.iso"
